package com.sx.wechat.controller.teacher;

import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.fastjson.JSONObject;
import com.sx.core.dao.InformationMapper;
import com.sx.core.model.Information;
import com.sx.core.mybatis.SessionTemplate;
import com.sx.core.utils.UUIDUtils;
import com.sx.core.vo.JsonReaderResponse;
import com.sx.wechat.controller.BaseController;
import com.sx.wechat.security.AuthenticationHolder;
import com.sx.wechat.shiro.InformationPrincipal;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by lihao on 2017/6/29.
 */
@RequestMapping("tc/class")
@RestController
public class ClassController extends BaseController {
    @Autowired
    InformationMapper informationMapper;




    @RequestMapping("list")
    public JsonReaderResponse list(@RequestBody JSONObject params) {
        int type=params.getInteger("type");
        if(type==1){
            return list_user_class(params);
        }
        return list_all_class(params);
    }
     @RequestMapping("list_all_class")
    public JsonReaderResponse list_all_class(@RequestBody JSONObject params) {
        InformationPrincipal information= AuthenticationHolder.getCurrentInfo();
        String keyword=params.getString("keyword");
         int page= NumberUtils.toInt(ObjectUtils.toString(params.get("page")),1);
         int limit= NumberUtils.toInt(ObjectUtils.toString(params.get("limit")),25);
         params.put("start",(page-1)*limit);

        String sql="select * from t_class ";

        if(StringUtils.isNotBlank(keyword)){
            sql+=" AND name LIKE  CONCAT('%',:keyword,'%') ) ";
        }

        String count_sql= PagerUtils.count(sql, JdbcConstants.MYSQL);
        long count = namedParameterJdbcTemplate.queryForObject(count_sql,params,Long.class);

        sql+=" ORDER BY createtime DESC  limit :start,:limit";
        List<Map<String,Object>> list=namedParameterJdbcTemplate.queryForList(sql,params);
         for(Map<String,Object> map:list){
             String check="select count(*) FROM t_class_teacher WHERE teacherid=? AND classid=?";
             map.put("user_yy",jdbcTemplate.queryForObject(check,Long.class,information.getId(),map.get("id"))>0);
         }

        return new JsonReaderResponse(list,count,page,limit).addExtra(informationMapper.get(Information.class,information.getId()));
    }

    @RequestMapping("list_user_class")
    public JsonReaderResponse list_user_class(@RequestBody JSONObject params) {
        InformationPrincipal information= AuthenticationHolder.getCurrentInfo();
        String keyword=params.getString("keyword");
        params.put("teacherid",information.getId());
        int page= NumberUtils.toInt(ObjectUtils.toString(params.get("page")),1);
        int limit= NumberUtils.toInt(ObjectUtils.toString(params.get("limit")),25);
        params.put("start",(page-1)*limit);

        String sql="select c.*,ct.teacherid from t_class_teacher ct JOIN t_class c ON ct.classid=c.id WHERE ct.teacherid=:teacherid";

        if(StringUtils.isNotBlank(keyword)){
            sql+=" AND c.name LIKE  CONCAT('%',:keyword,'%') ) ";
        }
        String count_sql= PagerUtils.count(sql, JdbcConstants.MYSQL);
        long count = namedParameterJdbcTemplate.queryForObject(count_sql,params,Long.class);

        sql+=" ORDER BY ct.createdate DESC  limit :start,:limit";
        List<Map<String,Object>> list=namedParameterJdbcTemplate.queryForList(sql,params);
        return new JsonReaderResponse(list,count,page,limit).addExtra(informationMapper.get(Information.class,information.getId()));
    }
     @RequestMapping("list_class_student")
    public JsonReaderResponse list_class_student(@RequestBody JSONObject params) {
        InformationPrincipal information= AuthenticationHolder.getCurrentInfo();
        String keyword=params.getString("keyword");
        params.put("teacherid",information.getId());

        String sql="SELECT * FROM t_information WHERE classid=:classid";

        if(StringUtils.isNotBlank(keyword)){
            sql+=" AND c.name LIKE  CONCAT('%',:keyword,'%') ) ";
        }
//        String count_sql= PagerUtils.count(sql, JdbcConstants.MYSQL);
//        long count = namedParameterJdbcTemplate.queryForObject(count_sql,params,Long.class);

//        sql+="  limit :page,:limit";
        List<Map<String,Object>> list=namedParameterJdbcTemplate.queryForList(sql,params);
        int page= NumberUtils.toInt(ObjectUtils.toString(params.get("page")),1);
        int limit= NumberUtils.toInt(ObjectUtils.toString(params.get("limit")),25);
        return new JsonReaderResponse(list,list.size(),page,limit).addExtra(informationMapper.get(Information.class,information.getId()));
    }


    @RequestMapping("add_user_class")
    public JsonReaderResponse list_student(@RequestBody JSONObject params) {
        InformationPrincipal information= AuthenticationHolder.getCurrentInfo();
        params.put("id", UUIDUtils.generateUuid32());
        params.put("createdate", new Date());
        params.put("teacherid",information.getId());
        namedParameterJdbcTemplate.update("DELETE FROM t_class_teacher WHERE classid=:classid AND teacherid=:teacherid",params);
        namedParameterJdbcTemplate.update("insert into t_class_teacher (id,teacherid,classid,createdate) values (:id,:teacherid,:classid,:createdate);",params);
        namedParameterJdbcTemplate.update("UPDATE t_class SET yy=yy+1 WHERE id=:classid;",params);
        return new JsonReaderResponse();
    }
    @RequestMapping("del_user_class")
    public JsonReaderResponse del_user_class(@RequestBody JSONObject params) {
        InformationPrincipal information= AuthenticationHolder.getCurrentInfo();
        params.put("teacherid",information.getId());
        namedParameterJdbcTemplate.update("DELETE FROM t_class_teacher WHERE classid=:classid AND teacherid=:teacherid",params);
        return new JsonReaderResponse();
    }














}
